package com.example.ycdyus.dailyfruit;

import android.app.Application;
import android.widget.Toast;

import com.example.ycdyus.dailyfruit.constant.AppConfig;
import com.example.ycdyus.dailyfruit.net.NetChangeObserver;
import com.example.ycdyus.dailyfruit.net.NetWorkUtil;
import com.example.ycdyus.dailyfruit.net.NetworkStateReceiver;
import com.example.ycdyus.dailyfruit.utils.FileUtil;
import com.example.ycdyus.dailyfruit.utils.FileUtils;
import com.example.ycdyus.dailyfruit.utils.ImageLoaderUtils;
import com.example.ycdyus.dailyfruit.utils.XutilsHelper;
import com.lidroid.xutils.DbUtils;

import cn.sharesdk.framework.ShareSDK;


/**
 * Created by ycdyus on 2015/10/9.
 */
public class BaseApp extends Application{
    private static BaseApp app;
    /**
     * 网络状态  observer
     */
    private static NetChangeObserver observer = new NetChangeObserver(){
        @Override
        public void onConnect(NetWorkUtil.NetType type) {
            super.onConnect(type);
            switch (type) {
                case WIFI:
                    showToast("亲!WIFI已连接");
                    break;
                case GNET_3:
                    showToast("移动3G网络已连接!!!");
                    break;
                default:
                    showToast("亲!网络已连接");
                    break;
            }
        }

        @Override
        public void onDisConnect() {
            super.onDisConnect();
            showToast("网络未连接");
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        initNetState();
        //配置ImageLoader
        ImageLoaderUtils.initImageLoader(app);
        //配置ShareSDK
        ShareSDK.initSDK(this);
        //数据库帮助类初始化
        XutilsHelper.init(this);
    }

    /**
     * 初始化网络监听
     */
    private void initNetState() {
        NetworkStateReceiver.registerNetworkStateReceiver(this);
        NetworkStateReceiver.registerObserver(observer);
    }

    /**
     * 注销网络监听
     */
    public static void unegisterNetworkStateReceiver() {
        NetworkStateReceiver.removeRegisterObserver(observer);
        NetworkStateReceiver.unRegisterNetworkStateReceiver(app);
    }
    /**
     * 弹出Toast
     */
    public static void showToast(String msg) {
        Toast.makeText(app, msg, Toast.LENGTH_SHORT).show();
    }

    public static BaseApp getInstance() {
        return app;
    }


    public DbUtils getDbUtils() {
        DbUtils.DaoConfig config = new DbUtils.DaoConfig(this);
        config.setDbName(AppConfig.DB_NAME);
        config.setDbVersion(AppConfig.DB_VERSION);
        config.setDbDir(FileUtils.getDBPath(this));
        config.setDbUpgradeListener(new DbUtils.DbUpgradeListener() {
            @Override
            public void onUpgrade(DbUtils db, int oldVersion, int newVersion) {

            }
        });
        return DbUtils.create(config);
    }
}
